<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-flash.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
    
  
  <link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  

  
    
      
    

    
  

  
    
    
    <link href="https://fonts.cat.net/css?family=Lato:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="XLForm," />










<meta name="description" content="简述XLForm 应该算是 iOS 平台上最灵活且最强大的动态表单库了, 以下是简单的结构图">
<meta name="keywords" content="XLForm">
<meta property="og:type" content="article">
<meta property="og:title" content="XLForm 表单库使用说明">
<meta property="og:url" content="http:&#x2F;&#x2F;urm9ril.top&#x2F;2017&#x2F;05&#x2F;02&#x2F;XLForm-usage&#x2F;index.html">
<meta property="og:site_name" content="ooops 的小站">
<meta property="og:description" content="简述XLForm 应该算是 iOS 平台上最灵活且最强大的动态表单库了, 以下是简单的结构图">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-uml.png">
<meta property="og:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-init.png">
<meta property="og:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-sample-custom-bad-keyboard.png">
<meta property="og:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-sample-custom.png">
<meta property="og:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-sample-custom-end.png">
<meta property="og:updated_time" content="2018-06-25T09:37:55.041Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http:&#x2F;&#x2F;q0du9mvt6.bkt.clouddn.com&#x2F;images&#x2F;XLForm&#x2F;xlform-uml.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":true,"transition":{"post_block":"perspectiveLeftIn","post_header":"fadeIn","post_body":"fadeIn","coll_header":"perspectiveLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://urm9ril.top/2017/05/02/XLForm-usage/"/>





  <title>XLForm 表单库使用说明 | ooops 的小站</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">ooops 的小站</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><_`_></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" target="_blank" rel="noopener" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://urm9ril.top/2017/05/02/XLForm-usage/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="张强">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ooops 的小站">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">XLForm 表单库使用说明</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-02T15:35:30+08:00">
                2017-05-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/XLForm/" itemprop="url" rel="index">
                    <span itemprop="name">XLForm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/02/XLForm-usage/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2017/05/02/XLForm-usage/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2017/05/02/XLForm-usage/" class="leancloud_visitors" data-flag-title="XLForm 表单库使用说明">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">热度&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
                 <span>℃</span>
             </span>
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,594
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  14
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="简述"><a href="#简述" class="headerlink" title="简述"></a>简述</h2><p><code>XLForm</code> 应该算是 <code>iOS</code> 平台上最灵活且最强大的动态表单库了, 以下是简单的结构图</p>
<img src="http://q0du9mvt6.bkt.clouddn.com/images/XLForm/xlform-uml.png">
<a id="more"></a>
<h3 id="最主要的上图红色框里的三个类文件"><a href="#最主要的上图红色框里的三个类文件" class="headerlink" title="最主要的上图红色框里的三个类文件"></a>最主要的上图红色框里的三个类文件</h3><div class="note info"><p><code>XLFormRowDescriptor</code>，<code>XLFormSectionDescriptor</code>，<code>XLFormDescriptor</code> </p></div>
<p><span id="inline-toc">1.</span><code>XLFormDescriptor</code>结构和 <code>UITablView</code> 一样，有 <code>Section</code> ,有 <code>Row</code>,它就是为成为 <code>UITableView</code> 的数据源而设计的.</p>
<p><span id="inline-toc">2.</span><code>XLFormRowDescriptor</code> 定义了每行表单的数据内容,包括行样式,标题,行类型,选择项内容,标签,合法性验证等.</p>
<p><span id="inline-toc">3.</span><code>XLFormSectionDescriptor</code> 是由 <code>XLFormRowDescriptor</code> 组合而成的,而 <code>XLFormSectionDescriptor</code> 最终又组成了 <code>XLFormDescriptor</code>.</p>
<h2 id="简单用法"><a href="#简单用法" class="headerlink" title="简单用法"></a>简单用法</h2><p><span id="inline-toc">1.</span> 创建一个继承自 <code>XLFormViewController</code> 的 <code>ViewController</code></p>
<p><span id="inline-toc">2.</span> 编写创建表单的代码</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)initForm &#123;</span><br><span class="line">    <span class="comment">// 创建 `XLFormDescriptor` 对象,等同于创建了 `UITableView`</span></span><br><span class="line">    XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:<span class="string">@"First Form"</span>];</span><br><span class="line">    <span class="comment">// 创建 `XLFormSectionDescriptor` 组对象</span></span><br><span class="line">    XLFormSectionDescriptor *section = [XLFormSectionDescriptor formSectionWithTitle:<span class="string">@"First Section"</span>];</span><br><span class="line">    <span class="comment">// 创建 `XLFormRowDescriptor` 对象,相当于 `UITableViewCell`</span></span><br><span class="line">    <span class="comment">// `XLFormRowDescriptorTypePhone` 是 `XLForm` 框架自带的 `XLFormRowDescriptorType` 类型</span></span><br><span class="line">    <span class="comment">// 具体参见文件 `XLForm.h` 头文件</span></span><br><span class="line">    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:<span class="string">@"PhoneRow"</span> rowType:XLFormRowDescriptorTypePhone];</span><br><span class="line">    <span class="comment">// 组内添加 `XLFormRowDescriptor` 对象</span></span><br><span class="line">    [section addFormRow:row];</span><br><span class="line">    <span class="comment">// 表单添加 `XLFormSectionDescriptor` 组对象</span></span><br><span class="line">    [form addFormSection:section];</span><br><span class="line">    <span class="comment">// 赋值创建的表单, `self.form` 是当前控制器持有的 `XLFormDescriptor` 对象</span></span><br><span class="line">    <span class="keyword">self</span>.form = form;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="keyword">super</span> viewDidLoad];</span><br><span class="line">    [<span class="keyword">self</span> initForm];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>Command + R</code> 效果如下</p>
<img src="http://q0du9mvt6.bkt.clouddn.com/images/XLForm/xlform-init.png">
<h3 id="具体的-XLFormRowDescriptorType"><a href="#具体的-XLFormRowDescriptorType" class="headerlink" title="具体的 XLFormRowDescriptorType"></a>具体的 XLFormRowDescriptorType</h3><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeText = <span class="string">@"text"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeName = <span class="string">@"name"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeURL = <span class="string">@"url"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeEmail = <span class="string">@"email"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypePassword = <span class="string">@"password"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeNumber = <span class="string">@"number"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypePhone = <span class="string">@"phone"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeTwitter = <span class="string">@"twitter"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeAccount = <span class="string">@"account"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeInteger = <span class="string">@"integer"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeImage = <span class="string">@"image"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDecimal = <span class="string">@"decimal"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeTextView = <span class="string">@"textView"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeZipCode = <span class="string">@"zipCode"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorPush = <span class="string">@"selectorPush"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorPopover = <span class="string">@"selectorPopover"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorActionSheet = <span class="string">@"selectorActionSheet"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorAlertView = <span class="string">@"selectorAlertView"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorPickerView = <span class="string">@"selectorPickerView"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorPickerViewInline = <span class="string">@"selectorPickerViewInline"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeMultipleSelector = <span class="string">@"multipleSelector"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeMultipleSelectorPopover = <span class="string">@"multipleSelectorPopover"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorLeftRight = <span class="string">@"selectorLeftRight"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSelectorSegmentedControl = <span class="string">@"selectorSegmentedControl"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDateInline = <span class="string">@"dateInline"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDateTimeInline = <span class="string">@"datetimeInline"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeTimeInline = <span class="string">@"timeInline"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeCountDownTimerInline = <span class="string">@"countDownTimerInline"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDate = <span class="string">@"date"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDateTime = <span class="string">@"datetime"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeTime = <span class="string">@"time"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeCountDownTimer = <span class="string">@"countDownTimer"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeDatePicker = <span class="string">@"datePicker"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypePicker = <span class="string">@"picker"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeSlider = <span class="string">@"slider"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeBooleanCheck = <span class="string">@"booleanCheck"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeBooleanSwitch = <span class="string">@"booleanSwitch"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeButton = <span class="string">@"button"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeInfo = <span class="string">@"info"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> XLFormRowDescriptorTypeStepCounter = <span class="string">@"stepCounter"</span>;</span><br></pre></td></tr></table></figure>
<p>位于框架内 <code>XLForm.h</code> 头文件中,请自行参看</p>
<h2 id="自定义-cell"><a href="#自定义-cell" class="headerlink" title="自定义 cell"></a>自定义 cell</h2><p>当框架自带的 XLFormRowDescriptorType 无法满足实际需求的时候,我们需要自定义.</p>
<h3 id="如何自定义一个-XLFormRowDescriptorType"><a href="#如何自定义一个-XLFormRowDescriptorType" class="headerlink" title="如何自定义一个 XLFormRowDescriptorType"></a>如何自定义一个 <code>XLFormRowDescriptorType</code></h3><p>我们必须重写如下三个方法:<br><span id="inline-toc">1.</span>load</p>
<p>注册自定义的 <code>cell</code>.</p>
<p><span id="inline-toc">2.</span>configure</p>
<p>初始化一些 <code>cell</code> 配置信息.</p>
<p><span id="inline-toc">3.</span>update</p>
<p>更新 <code>XLFromRowDescriptor</code> 对象的 <code>value</code> 属性值.</p>
<h3 id="如何自定义-cell"><a href="#如何自定义-cell" class="headerlink" title="如何自定义 cell"></a>如何自定义 cell</h3><p>下面演示如何自定义一个上边 <code>UILabel</code> 下边 <code>UITextField</code> 的自定义 <code>cell</code>, 用到了 <code>Masonry</code> 老司机自行导入.</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"SampleCustomCell.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLForm/XLForm.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;Masonry.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> SampleCustomCellType = <span class="string">@"SampleCustomCellType"</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">SampleCustomCell</span>()&lt;<span class="title">UITextFieldDelegate</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/** Label for display tip */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) <span class="built_in">UILabel</span>           *tipLabel;</span><br><span class="line"><span class="comment">/** textField for Receive input text */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) <span class="built_in">UITextField</span>       *inputTextField;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">SampleCustomCell</span></span></span><br><span class="line"></span><br><span class="line">+ (<span class="keyword">void</span>)load &#123;</span><br><span class="line">    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[<span class="keyword">self</span> <span class="keyword">class</span>] forKey:SampleCustomCellType];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)configure &#123;</span><br><span class="line">    [<span class="keyword">super</span> configure];</span><br><span class="line">    [<span class="keyword">self</span> setupUI];</span><br><span class="line">    [<span class="keyword">self</span> makeConstraint];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)update &#123;</span><br><span class="line">    [<span class="keyword">super</span> update];</span><br><span class="line">    <span class="keyword">self</span>.textLabel.text = <span class="keyword">self</span>.rowDescriptor.title;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#pragma mark - =========================================== layout ===================================</span></span><br><span class="line">- (<span class="keyword">void</span>)setupUI &#123;</span><br><span class="line">    [<span class="keyword">self</span>.contentView addSubview:<span class="keyword">self</span>.tipLabel];</span><br><span class="line">    [<span class="keyword">self</span>.contentView addSubview:<span class="keyword">self</span>.inputTextField];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)makeConstraint &#123;</span><br><span class="line">    [<span class="keyword">self</span>.textLabel mas_makeConstraints:^(MASConstraintMaker *make) &#123;</span><br><span class="line">        make.top.equalTo(<span class="keyword">self</span>.contentView).offset(<span class="number">10</span>);</span><br><span class="line">        make.left.equalTo(<span class="keyword">self</span>.contentView).offset(<span class="number">20</span>);</span><br><span class="line">    &#125;];</span><br><span class="line">    </span><br><span class="line">    [<span class="keyword">self</span>.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) &#123;</span><br><span class="line">        make.left.equalTo(<span class="keyword">self</span>.textLabel);</span><br><span class="line">        make.top.equalTo(<span class="keyword">self</span>.textLabel.mas_bottom).offset(<span class="number">10</span>);</span><br><span class="line">        make.right.equalTo(<span class="keyword">self</span>.contentView).offset(<span class="number">-20</span>);</span><br><span class="line">        make.height.mas_equalTo(<span class="number">44</span>);</span><br><span class="line">        make.bottom.equalTo(<span class="keyword">self</span>.contentView).offset(<span class="number">-10</span>);</span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#pragma mark - =========================================== lazy load ===================================</span></span><br><span class="line">- (<span class="built_in">UILabel</span> *)tipLabel &#123;</span><br><span class="line">    <span class="keyword">if</span>(_tipLabel) <span class="keyword">return</span> _tipLabel;</span><br><span class="line">    _tipLabel = [<span class="built_in">UILabel</span> new];</span><br><span class="line">    _tipLabel.textColor = HEXCOLOR(<span class="number">0xFF8A00</span>);</span><br><span class="line">    <span class="keyword">return</span> _tipLabel;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="built_in">UITextField</span> *)inputTextField &#123;</span><br><span class="line">    <span class="keyword">if</span>(_inputTextField) <span class="keyword">return</span> _inputTextField;</span><br><span class="line">    _inputTextField = [<span class="built_in">UITextField</span> new];</span><br><span class="line">    _inputTextField.delegate = <span class="keyword">self</span>;</span><br><span class="line">    _inputTextField.layer.borderColor = HEXCOLOR(<span class="number">0xFF8A00</span>).CGColor;</span><br><span class="line">    _inputTextField.layer.borderWidth = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> _inputTextField;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)setSelected:(<span class="built_in">BOOL</span>)selected animated:(<span class="built_in">BOOL</span>)animated &#123;</span><br><span class="line">    [<span class="keyword">super</span> setSelected:selected animated:animated];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure>
<img src="http://q0du9mvt6.bkt.clouddn.com/images/XLForm/xlform-sample-custom-bad-keyboard.png">
<p>但是键盘有问题,选中的背景色有问题,需要改造一下.<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)configure &#123;</span><br><span class="line">    [<span class="keyword">super</span> configure];</span><br><span class="line">    [<span class="keyword">self</span> setupUI];</span><br><span class="line">    [<span class="keyword">self</span> makeConstraint];</span><br><span class="line">    [<span class="keyword">self</span> setSelectionStyle:<span class="built_in">UITableViewCellSelectionStyleNone</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">.......</span><br><span class="line"></span><br><span class="line">- (<span class="built_in">BOOL</span>)formDescriptorCellCanBecomeFirstResponder &#123;</span><br><span class="line">    <span class="keyword">return</span> (!<span class="keyword">self</span>.rowDescriptor.isDisabled);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="built_in">BOOL</span>)formDescriptorCellBecomeFirstResponder &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.inputTextField becomeFirstResponder];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br><img src="http://q0du9mvt6.bkt.clouddn.com/images/XLForm/xlform-sample-custom.png"></p>
<p>好了,简单的自定义完成了.</p>
<h2 id="表单数据的获取"><a href="#表单数据的获取" class="headerlink" title="表单数据的获取"></a>表单数据的获取</h2><p>在 <code>viewController</code> 中<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="keyword">self</span>.form formValues];</span><br></pre></td></tr></table></figure><br>获取表单的值,其返回值是字典类型<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">formValues = &#123;</span><br><span class="line">    PhoneRow = <span class="number">5158</span>;</span><br><span class="line">    SampleRow = <span class="string">"&lt;null&gt;"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>你没有看错,刚才我们自定义的 <code>cell</code> 的表单值是 <code>&quot;&lt;null&gt;&quot;</code> ,不是因为没有输入值,而是没有给 <code>rowDescriptor.value</code> 赋值,继续改造一下.</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 添加事件</span></span><br><span class="line">[_inputTextField addTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(textFieldDidChange:) forControlEvents:<span class="built_in">UIControlEventEditingChanged</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 赋值</span></span><br><span class="line">- (<span class="keyword">void</span>)textFieldDidChange:(<span class="built_in">UITextField</span> *)textField &#123;</span><br><span class="line">    <span class="keyword">self</span>.rowDescriptor.value = textField.text;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">formValues = &#123;</span><br><span class="line">    PhoneRow = <span class="string">"5158"</span>;</span><br><span class="line">    SampleRow = Ooops;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="数据合法性校验器-XLFormValidator"><a href="#数据合法性校验器-XLFormValidator" class="headerlink" title="数据合法性校验器 XLFormValidator"></a>数据合法性校验器 XLFormValidator</h2><p>上面的例子,再给加点料.<br>粗糙一点的数据合法性验证<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#pragma mark - Helper</span></span><br><span class="line">- (<span class="keyword">void</span>)textFieldDidChange:(<span class="built_in">UITextField</span> *)textField &#123;</span><br><span class="line">    <span class="built_in">UITextRange</span> *rang = textField.markedTextRange;</span><br><span class="line">    <span class="keyword">if</span> (rang == <span class="literal">nil</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span>(textField.text.length &gt; <span class="number">8</span> )&#123;</span><br><span class="line">            <span class="built_in">NSRange</span> range = <span class="built_in">NSMakeRange</span>(<span class="number">0</span>, <span class="number">8</span>);</span><br><span class="line">            textField.text = [textField.text substringWithRange:range];</span><br><span class="line">            <span class="keyword">self</span>.rowDescriptor.value = textField.text;</span><br><span class="line">            <span class="built_in">NSLog</span>(<span class="string">@"最多输入8个字符"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">self</span>.rowDescriptor.value = textField.text;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这么做其实也是可以的,但是框架提供了 <code>XLFormValidator</code> 数据校验类.</p>
<p>我们来看看如何使用</p>
<h3 id="XLFormValidator-的创建"><a href="#XLFormValidator-的创建" class="headerlink" title="XLFormValidator 的创建"></a>XLFormValidator 的创建</h3><p>创建一个继承自 <code>XLFormValidator</code> 的 <code>SampleValidator</code> 类文件,使用正则表达式的方式验证数据合法性.<br><code>.h</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"XLFormValidator.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">SampleValidator</span> : <span class="title">XLFormValidator</span></span></span><br><span class="line"></span><br><span class="line">+ (XLFormValidator *)sampleValidator;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure><br><code>.m</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"SampleValidator.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"XLFormRegexValidator.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">SampleValidator</span></span></span><br><span class="line"></span><br><span class="line">- (XLFormValidationStatus *)isValid:(XLFormRowDescriptor *)row &#123;</span><br><span class="line">    <span class="keyword">return</span> [XLFormValidationStatus formValidationStatusWithMsg:<span class="literal">nil</span> status:<span class="literal">YES</span> rowDescriptor:row];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">+ (XLFormValidator *)sampleValidator &#123;</span><br><span class="line">    <span class="keyword">return</span> [XLFormRegexValidator formRegexValidatorWithMsg:<span class="built_in">NSLocalizedString</span>(<span class="string">@"最多输入8个字符"</span>, <span class="literal">nil</span>) regex:<span class="string">@"^.&#123;0,8&#125;$"</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<h3 id="XLFormValidator-的使用"><a href="#XLFormValidator-的使用" class="headerlink" title="XLFormValidator 的使用"></a>XLFormValidator 的使用</h3><p>在 <code>viewController</code> 中 为先前创建的自定义 <code>cell</code> 添加刚创建的 <code>数据合法性</code> 校验器,且设为 <code>必填项</code> .<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"SampleValidator.h"</span></span></span><br><span class="line">......</span><br><span class="line">[sampleRow addValidator:[SampleValidator sampleValidator]];</span><br><span class="line">sampleRow.required = <span class="literal">YES</span>;</span><br></pre></td></tr></table></figure><br>继续改造一下代码.</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)values:(<span class="built_in">UIButton</span> *)sender &#123;</span><br><span class="line">    <span class="keyword">if</span>(![<span class="keyword">self</span> validatorAction]) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">NSDictionary</span> *values = [<span class="keyword">self</span>.form formValues];</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"formValues = %@"</span>,values);</span><br><span class="line">    <span class="comment">// 接口调用...balabalabala</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#pragma mark - =========================================== 数据合法性校验 ===================================</span></span><br><span class="line">- (<span class="built_in">BOOL</span>)validatorAction &#123;</span><br><span class="line">    <span class="built_in">NSArray</span> * array = [<span class="keyword">self</span>.form localValidationErrors:<span class="keyword">self</span>];</span><br><span class="line">    <span class="built_in">BOOL</span> isValidator;</span><br><span class="line">    <span class="keyword">if</span>(array.count &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        [array enumerateObjectsUsingBlock:^(<span class="keyword">id</span> obj, <span class="built_in">NSUInteger</span> idx, <span class="built_in">BOOL</span> *stop) &#123;</span><br><span class="line">            XLFormValidationStatus * validationStatus = [[obj userInfo] objectForKey:XLValidationStatusErrorKey];</span><br><span class="line">            <span class="keyword">if</span> ([validationStatus.rowDescriptor.tag isEqualToString:sampleRowTag] &amp;&amp; !validationStatus.isValid) &#123;</span><br><span class="line">                *stop = <span class="literal">YES</span>;</span><br><span class="line">                <span class="built_in">NSLog</span>(<span class="string">@"请修正数据"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">UITableViewCell</span> * cell = [<span class="keyword">self</span>.tableView cellForRowAtIndexPath:[<span class="keyword">self</span>.form indexPathOfFormRow:validationStatus.rowDescriptor]];</span><br><span class="line">            [<span class="keyword">self</span> animateCell:cell];</span><br><span class="line">        &#125;];</span><br><span class="line">        isValidator = <span class="literal">NO</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        isValidator = <span class="literal">YES</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> isValidator;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 验证数据错误的cell动画</span></span><br><span class="line">- (<span class="keyword">void</span>)animateCell:(<span class="built_in">UITableViewCell</span> *)cell &#123;</span><br><span class="line">    <span class="built_in">CAKeyframeAnimation</span> *animation = [<span class="built_in">CAKeyframeAnimation</span> animation];</span><br><span class="line">    animation.keyPath = <span class="string">@"position.x"</span>;</span><br><span class="line">    animation.values =  @[ @<span class="number">0</span>, @<span class="number">20</span>, @<span class="number">-20</span>, @<span class="number">10</span>, @<span class="number">0</span>];</span><br><span class="line">    animation.keyTimes = @[@<span class="number">0</span>, @(<span class="number">1</span> / <span class="number">6.0</span>), @(<span class="number">3</span> / <span class="number">6.0</span>), @(<span class="number">5</span> / <span class="number">6.0</span>), @<span class="number">1</span>];</span><br><span class="line">    animation.duration = <span class="number">0.3</span>;</span><br><span class="line">    animation.timingFunction = [<span class="built_in">CAMediaTimingFunction</span> functionWithName:kCAMediaTimingFunctionEaseOut];</span><br><span class="line">    animation.additive = <span class="literal">YES</span>;</span><br><span class="line">    </span><br><span class="line">    [cell.layer addAnimation:animation forKey:<span class="string">@"shake"</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>只是一个自定义 <code>cell</code> 控制器的代码已经有点多了.下面我们抽离一下</p>
<p><span id="inline-toc">1.</span> 抽离 <code>cell tag</code> 的常量定义到 <code>CustomConst</code> 文件中 .<br><code>.h</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">&lt;Foundation/Foundation.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="built_in">NSString</span> *<span class="keyword">const</span> phoneRowTag;</span><br><span class="line"><span class="keyword">extern</span> <span class="built_in">NSString</span> *<span class="keyword">const</span> sampleRowTag;</span><br></pre></td></tr></table></figure></p>
<p><code>.m</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"CustomConst.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> phoneRowTag = <span class="string">@"phoneRowTag"</span>;</span><br><span class="line"><span class="built_in">NSString</span> *<span class="keyword">const</span> sampleRowTag = <span class="string">@"sampleRowTag"</span>;</span><br></pre></td></tr></table></figure><br><span id="inline-toc">2.</span> 抽离 <code>initForm</code> 表单创建方法到 <code>CustomFactory</code> 文件<br><code>.h</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">&lt;Foundation/Foundation.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLFormViewController.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">CustomFactory</span> : <span class="title">NSObject</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/** delegate */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) XLFormViewController          *delegate;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** xlForm */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) XLFormDescriptor              *form;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> 初始化表单方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">- (<span class="keyword">void</span>)initForm;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<p><code>.m</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"CustomFactory.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"CustomConst.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"SampleValidator.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"SampleCustomCell.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLForm.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">CustomFactory</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)initForm &#123;</span><br><span class="line">    XLFormSectionDescriptor * sampleSection = [XLFormSectionDescriptor formSectionWithTitle:<span class="string">@"First Section"</span>];</span><br><span class="line">    </span><br><span class="line">    [sampleSection addFormRow:[<span class="keyword">self</span> rowFactoryWithWithTitle:<span class="string">@""</span></span><br><span class="line">                                                        tag:phoneRowTag</span><br><span class="line">                                                   required:<span class="literal">YES</span></span><br><span class="line">                                                    rowType:XLFormRowDescriptorTypePhone</span><br><span class="line">                                                placeholder:<span class="string">@""</span>]];</span><br><span class="line">    </span><br><span class="line">    [sampleSection addFormRow:[<span class="keyword">self</span> rowFactoryWithWithTitle:<span class="string">@"自定义的tip"</span></span><br><span class="line">                                                        tag:sampleRowTag</span><br><span class="line">                                                   required:<span class="literal">YES</span></span><br><span class="line">                                                    rowType:SampleCustomCellType</span><br><span class="line">                                                placeholder:<span class="string">@"最长八位字符"</span>]];</span><br><span class="line">    </span><br><span class="line">    [<span class="keyword">self</span>.form addFormSection:sampleSection];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (XLFormRowDescriptor *)rowFactoryWithWithTitle:(<span class="built_in">NSString</span> *)headerTitle tag:(<span class="built_in">NSString</span> *)tag required:(<span class="built_in">BOOL</span>)required rowType:(<span class="built_in">NSString</span> *)rowType placeholder:(<span class="built_in">NSString</span> *)placeholder &#123;</span><br><span class="line">    XLFormRowDescriptor *row;</span><br><span class="line">    row = [XLFormRowDescriptor formRowDescriptorWithTag:tag rowType:rowType];</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(tag == sampleRowTag) &#123;</span><br><span class="line">        [row addValidator:[SampleValidator sampleValidator]];</span><br><span class="line">        [row.cellConfigAtConfigure setObject:placeholder forKey:<span class="string">@"inputTextField.placeholder"</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(required) &#123;</span><br><span class="line">        row.requireMsg = <span class="string">@"带*为必填!"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    row.required = required;</span><br><span class="line">    row.title = headerTitle;</span><br><span class="line">    row.height = <span class="built_in">UITableViewAutomaticDimension</span>;</span><br><span class="line">    <span class="keyword">return</span> row;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<p><span id="inline-toc">3.</span> 抽离 <code>validatorAction</code> 数据合法校验方法 到 <code>CustomValidator</code> 文件中</p>
<p><code>.h</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">&lt;Foundation/Foundation.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;UIKit/UIKit.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLForm.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">CustomValidator</span> : <span class="title">NSObject</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/** 验证表格 */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) <span class="built_in">UITableView</span>                   *tableView;</span><br><span class="line"><span class="comment">/** xlForm */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) XLFormDescriptor                *form;</span><br><span class="line"><span class="comment">/** delegate */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) XLFormViewController            *delegate;</span><br><span class="line"><span class="comment">/** 所有数据是否校验完毕 */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>,<span class="keyword">assign</span>)  <span class="built_in">BOOL</span>                          isValidate;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 验证表单数据的方法 */</span></span><br><span class="line">- (<span class="keyword">void</span>)validatorAction;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<p><code>.m</code><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"CustomValidator.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"CustomConst.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">CustomValidator</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#pragma mark - cell数据合法化验证方法</span></span><br><span class="line">- (<span class="built_in">NSArray</span> *)formValidationErrors &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="keyword">self</span>.form localValidationErrors:<span class="keyword">self</span>.delegate];</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#pragma mark - 数据校验器</span></span><br><span class="line">- (<span class="keyword">void</span>)validatorAction &#123;</span><br><span class="line">    <span class="built_in">NSArray</span> * array = [<span class="keyword">self</span> formValidationErrors];</span><br><span class="line">    <span class="keyword">self</span>.isValidate = !(array.count &gt; <span class="number">0</span>);</span><br><span class="line">    [array enumerateObjectsUsingBlock:^(<span class="keyword">id</span> obj, <span class="built_in">NSUInteger</span> idx, <span class="built_in">BOOL</span> *stop) &#123;</span><br><span class="line">        XLFormValidationStatus * validationStatus = [[obj userInfo] objectForKey:XLValidationStatusErrorKey];</span><br><span class="line">        <span class="keyword">if</span> ([validationStatus.rowDescriptor.tag isEqualToString:sampleRowTag] &amp;&amp; !validationStatus.isValid) &#123;</span><br><span class="line">            *stop = <span class="literal">YES</span>;</span><br><span class="line">            <span class="built_in">NSLog</span>(<span class="string">@"请修正数据"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">UITableViewCell</span> * cell = [<span class="keyword">self</span>.tableView cellForRowAtIndexPath:[<span class="keyword">self</span>.form indexPathOfFormRow:validationStatus.rowDescriptor]];</span><br><span class="line">        [<span class="keyword">self</span> animateCell:cell];</span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 验证数据错误的cell动画</span></span><br><span class="line">- (<span class="keyword">void</span>)animateCell:(<span class="built_in">UITableViewCell</span> *)cell &#123;</span><br><span class="line">    <span class="built_in">CAKeyframeAnimation</span> *animation = [<span class="built_in">CAKeyframeAnimation</span> animation];</span><br><span class="line">    animation.keyPath = <span class="string">@"position.x"</span>;</span><br><span class="line">    animation.values =  @[ @<span class="number">0</span>, @<span class="number">20</span>, @<span class="number">-20</span>, @<span class="number">10</span>, @<span class="number">0</span>];</span><br><span class="line">    animation.keyTimes = @[@<span class="number">0</span>, @(<span class="number">1</span> / <span class="number">6.0</span>), @(<span class="number">3</span> / <span class="number">6.0</span>), @(<span class="number">5</span> / <span class="number">6.0</span>), @<span class="number">1</span>];</span><br><span class="line">    animation.duration = <span class="number">0.3</span>;</span><br><span class="line">    animation.timingFunction = [<span class="built_in">CAMediaTimingFunction</span> functionWithName:kCAMediaTimingFunctionEaseOut];</span><br><span class="line">    animation.additive = <span class="literal">YES</span>;</span><br><span class="line">    </span><br><span class="line">    [cell.layer addAnimation:animation forKey:<span class="string">@"shake"</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<p><code>viewController</code> 中的代码变成了这样<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"ViewController.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLForm/XLFormViewController.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;XLForm.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">&lt;Masonry.h&gt;</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"CustomFactory.h"</span></span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"CustomValidator.h"</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">ViewController</span> ()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/** button for get form values */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) <span class="built_in">UIButton</span>              *valuesButton;</span><br><span class="line"><span class="comment">/** custom factory */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) CustomFactory         *customFactory;</span><br><span class="line"><span class="comment">/** Custom Validator */</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">strong</span>) CustomValidator       *customValidator;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">ViewController</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="keyword">super</span> viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    [<span class="keyword">self</span> initForm];</span><br><span class="line">    [<span class="keyword">self</span> setupUI];</span><br><span class="line">    [<span class="keyword">self</span> makeConstraint];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)setupUI &#123;</span><br><span class="line">    [<span class="keyword">self</span>.view addSubview:<span class="keyword">self</span>.valuesButton];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)makeConstraint &#123;</span><br><span class="line">    [<span class="keyword">self</span>.tableView mas_makeConstraints:^(MASConstraintMaker *make) &#123;</span><br><span class="line">        make.left.top.right.equalTo(<span class="keyword">self</span>.view);</span><br><span class="line">        make.bottom.mas_equalTo(<span class="number">-100</span>);</span><br><span class="line">    &#125;];</span><br><span class="line">    </span><br><span class="line">    [<span class="keyword">self</span>.valuesButton mas_makeConstraints:^(MASConstraintMaker *make) &#123;</span><br><span class="line">        make.left.right.bottom.equalTo(<span class="keyword">self</span>.view);</span><br><span class="line">        make.height.mas_equalTo(<span class="number">50</span>);</span><br><span class="line">    &#125;];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)initForm &#123;</span><br><span class="line">    <span class="keyword">self</span>.form = [XLFormDescriptor formDescriptorWithTitle:<span class="string">@"First Form"</span>];</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">self</span>.customFactory.form = <span class="keyword">self</span>.form;</span><br><span class="line">    <span class="keyword">self</span>.customFactory.delegate = <span class="keyword">self</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">self</span>.customValidator.form = <span class="keyword">self</span>.form;</span><br><span class="line">    <span class="keyword">self</span>.customValidator.delegate = <span class="keyword">self</span>;</span><br><span class="line">    <span class="keyword">self</span>.customValidator.tableView = <span class="keyword">self</span>.tableView;</span><br><span class="line">    </span><br><span class="line">    [<span class="keyword">self</span>.customFactory initForm];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#pragma mark - =========================================== lazy load ===================================</span></span><br><span class="line"></span><br><span class="line">- (<span class="built_in">UIButton</span> *)valuesButton &#123;</span><br><span class="line">    <span class="keyword">if</span>(_valuesButton) <span class="keyword">return</span> _valuesButton;</span><br><span class="line">    _valuesButton = [<span class="built_in">UIButton</span> buttonWithType:<span class="number">0</span>];</span><br><span class="line">    [_valuesButton setTitle:<span class="string">@"Values"</span> forState:<span class="built_in">UIControlStateNormal</span>];</span><br><span class="line">    [_valuesButton setBackgroundColor:HEXCOLOR(<span class="number">0xFF8A00</span>)];</span><br><span class="line">    [_valuesButton setTitleColor:HEXCOLOR(<span class="number">0xFFFFFF</span>) forState:<span class="built_in">UIControlStateNormal</span>];</span><br><span class="line">    [_valuesButton addTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(values:) forControlEvents:<span class="built_in">UIControlEventTouchUpInside</span>];</span><br><span class="line">    <span class="keyword">return</span> _valuesButton;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (CustomFactory *)customFactory &#123;</span><br><span class="line">    <span class="keyword">if</span>(_customFactory) <span class="keyword">return</span> _customFactory;</span><br><span class="line">    _customFactory = [CustomFactory new];</span><br><span class="line">    <span class="keyword">return</span> _customFactory;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (CustomValidator *)customValidator &#123;</span><br><span class="line">    <span class="keyword">if</span>(_customValidator) <span class="keyword">return</span> _customValidator;</span><br><span class="line">    _customValidator = [CustomValidator new];</span><br><span class="line">    <span class="keyword">return</span> _customValidator;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)values:(<span class="built_in">UIButton</span> *)sender &#123;</span><br><span class="line">    [<span class="keyword">self</span>.customValidator validatorAction];</span><br><span class="line">    <span class="keyword">if</span>(!<span class="keyword">self</span>.customValidator.isValidate) <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">NSDictionary</span> *values = [<span class="keyword">self</span>.form formValues];</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"formValues = %@"</span>,values);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)didReceiveMemoryWarning &#123;</span><br><span class="line">    [<span class="keyword">super</span> didReceiveMemoryWarning];</span><br><span class="line">    <span class="comment">// Dispose of any resources that can be recreated.</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure><br><img src="http://q0du9mvt6.bkt.clouddn.com/images/XLForm/xlform-sample-custom-end.png"></p>
<p>所有跟表单有关的东西都抽离出去. 有种 <code>万花丛中过,便宜不沾身</code> 的感觉,有没有.</p>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>后面有机会介绍更加复杂的自定义 <code>cell</code>…</p>
<p>补上长久以来缺失的 <a href="https://github.com/urm9ril/XLFormUsage" target="_blank" rel="noopener">Demo</a></p>

      
    </div>
    
    
    

    

    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/2017/05/02/XLForm-usage/">XLForm 表单库使用说明</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 张强 的个人博客">张强</a></p>
  <p><span>发布时间:</span>2017年05月02日 - 15:05</p>
  <p><span>最后更新:</span>2018年06月25日 - 17:06</p>
  <p><span>原始链接:</span><a href="/2017/05/02/XLForm-usage/" title="XLForm 表单库使用说明">http://urm9ril.top/2017/05/02/XLForm-usage/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://urm9ril.top/2017/05/02/XLForm-usage/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      
    </div>

    

    

    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束  <i class="fa-window-minimize"></i>  ß感谢您的阅读-------------</div>
    
</div>
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/XLForm/" <i class="fa fa-tags"></i>XLForm</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/12/Jenkins/" rel="next" title="Jenkins一键发布 ipa 到蒲公英">
                <i class="fa fa-chevron-left"></i> Jenkins一键发布 ipa 到蒲公英
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/25/hexo-domain-alicloud/" rel="prev" title="Hexo-域名解析(阿里云)">
                Hexo-域名解析(阿里云) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">张强</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

            <div id="days"></div>
    </script>
    <script language="javascript">
    function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay = new Date("06/10/2014 15:00:00");
        today = new Date();
        timeold = (today.getTime() - BirthDay.getTime());
        sectimeold = timeold / 1000
        secondsold = Math.floor(sectimeold);
        msPerDay = 24 * 60 * 60 * 1000
        e_daysold = timeold / msPerDay
        daysold = Math.floor(e_daysold);
        e_hrsold = (e_daysold - daysold) * 24;
        hrsold = setzero(Math.floor(e_hrsold));
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = setzero(Math.floor((e_hrsold - hrsold) * 60));
        seconds = setzero(Math.floor((e_minsold - minsold) * 60));
        document.getElementById('days').innerHTML = "已运行" + daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
    }
    function setzero(i) {
        if (i<10)
        {i="0" + i};
        return i;
    }
    show_date_time();
</script>

          </nav>

          

          

          
          

          
          
        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简述"><span class="nav-number">1.</span> <span class="nav-text">简述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#最主要的上图红色框里的三个类文件"><span class="nav-number">1.1.</span> <span class="nav-text">最主要的上图红色框里的三个类文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#简单用法"><span class="nav-number">2.</span> <span class="nav-text">简单用法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#具体的-XLFormRowDescriptorType"><span class="nav-number">2.1.</span> <span class="nav-text">具体的 XLFormRowDescriptorType</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自定义-cell"><span class="nav-number">3.</span> <span class="nav-text">自定义 cell</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#如何自定义一个-XLFormRowDescriptorType"><span class="nav-number">3.1.</span> <span class="nav-text">如何自定义一个 XLFormRowDescriptorType</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何自定义-cell"><span class="nav-number">3.2.</span> <span class="nav-text">如何自定义 cell</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#表单数据的获取"><span class="nav-number">4.</span> <span class="nav-text">表单数据的获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据合法性校验器-XLFormValidator"><span class="nav-number">5.</span> <span class="nav-text">数据合法性校验器 XLFormValidator</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#XLFormValidator-的创建"><span class="nav-number">5.1.</span> <span class="nav-text">XLFormValidator 的创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#XLFormValidator-的使用"><span class="nav-number">5.2.</span> <span class="nav-text">XLFormValidator 的使用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">6.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2019</span>
  <!--<span class="with-love">
    <i class="fa fa-heart"></i>
  </span>-->
  <span class="with-love">
  (●'◡'●)ﾉ♥
  </span>
  <span class="author" itemprop="copyrightHolder">张强</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">全站总字数&#58;</span>
    
    <span title="全站总字数">16.3k</span>
  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      你是本站第<span class="busuanzi-value" id="busuanzi_value_site_uv"></span>位访客
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      文章共批阅<span class="busuanzi-value" id="busuanzi_value_site_pv"></span>次
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = '';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '7FNrT8jBTCLil5J7vFWmPVe3-gzGzoHsz',
        appKey: 'Evbd5Bv3xxN8M3GybtBOyHXU',
        placeholder: '畅所欲言,请尽可能留下真实的邮箱,以便交流.',
        avatar:'wavatar',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("7FNrT8jBTCLil5J7vFWmPVe3-gzGzoHsz", "Evbd5Bv3xxN8M3GybtBOyHXU");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "topCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  


  <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>;
  <script>
    $(document).ready(function(){
      $('body').backstretch([
        {url: "/images/background.png", fade: 4000},
        {url: "/images/background01.jpg", fade: 4000},
        {url: "/images/background02.jpg", fade: 4000},
        {url: "/images/background03.jpg", fade: 4000}
      ]);
    });
  </script> -->

</body>
</html>
